package com.loong.musicdemo.ui.find.activity.ui.detail.fragment;

import android.os.AsyncTask;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.loong.musicdemo.R;
import com.loong.musicdemo.model.Song;
import com.loong.musicdemo.ui.find.adapter.AlbumSongAdapter;
import com.loong.musicdemo.util.dataDeal.MusicDeal;
import com.loong.musicdemo.util.dataDeal.SingerDeal;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link SingerSongFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class SingerSongFragment extends Fragment {
    private RecyclerView singerSongsRecyclerView;
    private AlbumSongAdapter songAdapter;
    private List<Song> singerSongs = new ArrayList<>();
    private String singerId;

    public SingerSongFragment(String singerId) {
        this.singerId = singerId;
    }

    // TODO: Rename and change types and number of parameters
    public static SingerSongFragment newInstance(String singerId) {
        SingerSongFragment fragment = new SingerSongFragment(singerId);
        return fragment;
    }



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_singer_song, container, false);
        singerSongsRecyclerView = root.findViewById(R.id.singer_songs_recycler_view);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        singerSongsRecyclerView.setLayoutManager(layoutManager);
        songAdapter = new AlbumSongAdapter(singerSongs);
        singerSongsRecyclerView.setAdapter(songAdapter);
        new SongTask().execute();
        return root;
    }

    class SongTask extends AsyncTask<List<String>,Void,List<Song>>{
        @Override
        protected void onPostExecute(List<Song> songs) {
            super.onPostExecute(songs);
            singerSongs.clear();
            singerSongs.addAll(songs);
            songAdapter.notifyDataSetChanged();
        }

        @Override
        protected List<Song> doInBackground(List<String>... lists) {
            List<String> ids = SingerDeal.getSingerSongIds(singerId);
            return MusicDeal.getSongListByIds(ids);
        }
    }
}